﻿/*************************************************************************************
 * 文 件 名:   HK_ProjectPro
 * 
 * 描    述:  本工具用于在 Project 面板中为特定的文件夹显示自定义图标。这些文件夹包括但不限于脚本文件夹（Scripts）、预设体文件夹（Prefabs）、资源文件夹（Resources）等。该工具可以帮助开发者更直观地识别和管理项目中的不同类型文件夹。
 * 
 * 版    本：  V1.1
 * 创 建 者：  京产肠饭
 * 创建时间：  2023/11/20
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

namespace HKTools
{
    [InitializeOnLoad]
    public class HK_ProjectPro
    {
        public static void ChangeState(bool isActive)
        {
            if (isActive)
            {
                EditorApplication.projectWindowItemOnGUI += OnProjectWindowItemOnGUI;
            }
            else
            {
                EditorApplication.projectWindowItemOnGUI -= OnProjectWindowItemOnGUI;
            }

            EditorApplication.RepaintProjectWindow();
        }

        // 图标名称的常量
        const string IconScriptsLight = "Icon_Scripts_Light";
        const string IconScriptsDark = "Icon_Scripts_Dark";
        const string IconPrefabsLight = "Icon_Prefabs_Light";
        const string IconPrefabsDark = "Icon_Prefabs_Dark";
        const string IconResourcesLight = "Icon_Resources_Light";
        const string IconResourcesDark = "Icon_Resources_Dark";
        const string IconStreamingAssetsLight = "Icon_StreamingAssets_Light";
        const string IconStreamingAssetsDark = "Icon_StreamingAssets_Dark";
        const string IconScenesLight = "Icon_Scenes_Light";
        const string IconScenesDark = "Icon_Scenes_Dark";
        const string IconMaterialsLight = "Icon_Materials_Light";
        const string IconMaterialsDark = "Icon_Materials_Dark";
        const string IconUILight = "Icon_UI_Light";
        const string IconUIDark = "Icon_UI_Dark";

        static List<string> iconFolderNames = new List<string>()
        {
            "_scripts", "_script", "scripts", "script",
            "_models", "models",
            "_prefabs","prefabs","_prefab","prefab",
            "resources",
            "streamingassets",
            "scenes","_scenes","scene","_scene",
            "materials","material","mats","mat","_materials","_material","_mats","_mat",
            "ui","_ui","uis","_uis"
        };

        // 缓存的 Texture2D 图标
        static Dictionary<string, Texture2D> iconCache = new Dictionary<string, Texture2D>();

        // 为项目窗口中的每个可见的列表项调用
        static void OnProjectWindowItemOnGUI(string guid, Rect rect)
        {
            // 从 guid 获取资产的路径
            string path = AssetDatabase.GUIDToAssetPath(guid);

            // 如果路径指向一个文件夹
            if (AssetDatabase.IsValidFolder(path))
            {
                // 从路径获取文件夹的名称
                string folderName = System.IO.Path.GetFileName(path);

                folderName = folderName.ToLower();

                if (!iconFolderNames.Contains(folderName))
                    return;

                // 从路径获取文件夹的图标
                Texture2D icon = GetFolderIcon(folderName);

                // 如果图标不为空，就在矩形上绘制它
                if (icon != null)
                {
                    float scale = Mathf.Min(rect.width / icon.width, rect.height / icon.height);
                    Rect iconRect = new Rect(rect.x, rect.y, icon.width * scale, icon.height * scale);
                    GUI.DrawTexture(iconRect, icon);
                }
            }
        }

        // 这个方法从路径返回文件夹的图标
        static Texture2D GetFolderIcon(string folderName)
        {
            if (iconCache.TryGetValue(folderName, out Texture2D icon))
            {
                return icon;
            }

            string iconName = "";

            // 使用 switch 语句来匹配文件夹的名称和图标的名称
            if (EditorGUIUtility.isProSkin)
            {
                switch (folderName)
                {
                    case "_script":
                    case "_scripts":
                    case "script":
                    case "scripts":
                        iconName = IconScriptsLight;
                        break;
                    case "_prefabs":
                    case "prefabs":
                    case "_prefab":
                    case "prefab":
                        iconName = IconPrefabsLight;
                        break;
                    case "resources":
                        iconName = IconResourcesLight;
                        break;
                    case "streamingassets":
                        iconName = IconStreamingAssetsLight;
                        break;
                    case "_scenes":
                    case "scenes":
                    case "_scene":
                    case "scene":
                        iconName = IconScenesLight;
                        break;
                    case "materials":
                    case "material":
                    case "mats":
                    case "mat":
                    case "_materials":
                    case "_material":
                    case "_mats":
                    case "_mat":
                        iconName = IconMaterialsLight;
                        break;
                    case "ui":
                    case "_ui":
                    case "uis":
                    case "_uis":
                        iconName = IconUILight;
                        break;
                }
            }
            else
            {
                switch (folderName)
                {
                    case "_script":
                    case "_scripts":
                    case "script":
                    case "scripts":
                        iconName = IconScriptsDark;
                        break;
                    case "_prefabs":
                    case "prefabs":
                    case "_prefab":
                    case "prefab":
                        iconName = IconPrefabsDark;
                        break;
                    case "resources":
                        iconName = IconResourcesDark;
                        break;
                    case "streamingassets":
                        iconName = IconStreamingAssetsDark;
                        break;
                    case "_scenes":
                    case "scenes":
                    case "_scene":
                    case "scene":
                        iconName = IconScenesDark;
                        break;
                    case "materials":
                    case "material":
                    case "mats":
                    case "mat":
                    case "_materials":
                    case "_material":
                    case "_mats":
                    case "_mat":
                        iconName = IconMaterialsDark;
                        break;
                    case "ui":
                    case "_ui":
                    case "uis":
                    case "_uis":
                        iconName = IconUIDark;
                        break;
                }
            }

            // 如果名称不为空，就从资源文件夹中加载图标，并进行缓存
            if (!string.IsNullOrEmpty(iconName))
            {
                icon = Resources.Load<Texture2D>(iconName);
                if (icon != null)
                {
                    iconCache[folderName] = icon;
                }
            }

            return icon;
        }
    }
}